package com.wy.system.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.wy.common.annotation.DataScope;
import com.wy.common.core.domain.entity.SysUser;
import com.wy.common.utils.SecurityUtils;
import com.wy.system.domain.CarportView;
import com.wy.system.domain.SysHouse;
import com.wy.system.domain.SysStaff;
import com.wy.system.domain.vo.SysCarVo;
import com.wy.system.domain.vo.SysHouseVo;
import com.wy.system.domain.vo.SysPayVo;
import com.wy.system.domain.vo.SysStaffVo;
import com.wy.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.wy.system.domain.SysPay;
import com.wy.system.service.ISysPayService;

/**
 * 物业费记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-03-03
 */
@Service
@Primary
public class SysPayServiceImpl implements ISysPayService 
{
    @Autowired
    private SysPayMapper sysPayMapper;
    @Autowired
    private SysHouseMapper sysHouseMapper;
    @Autowired
    private SysCarMapper sysCarMapper;
    @Autowired
    private SysStaffMapper sysStaffMapper;
    @Autowired
    private SysUserMapper sysUserMapper;


    /**
     * 查询物业费记录
     * 
     * @param id 物业费记录主键
     * @return 物业费记录
     */
    @Override
    public SysPayVo selectSysPayById(Long id)
    {
        return sysPayMapper.selectSysPayById(id);
    }

    /**
     * 查询物业费记录列表
     * 
     * @param sysPay 物业费记录
     * @return 物业费记录
     */
    @Override
    @DataScope(userAlias = "u")
    public List<SysPayVo> selectSysPayList(SysPay sysPay)
    {
        //获取物业费记录列表
        List<SysPayVo> sysPays = sysPayMapper.selectSysPayList(sysPay);

        //遍历记录
        for (SysPayVo pay:sysPays){
            //记录的cid
            Long hcId = pay.getHcId();
            //记录的name
            String name=null;
            //查询每一个记录类型的对应的name
            switch (pay.getType()){
                //物业费
                case "0":
                    name = sysHouseMapper.selectSysHouseById(hcId).getLocation();
                    pay.setHcName(name);
                    break;
                //车位费
                case "1":
                    name=sysCarMapper.selectSysCarById(hcId).getPlate();
                    pay.setHcName(name);
                    break;
                //员工工资
                case "2":
                    name=sysUserMapper.selectUserById(hcId).getNickName();
                    pay.setHcName(name);
                    break;
            }
        }
        return sysPays;
    }

    /**
     * 新增物业费记录
     * 
     * @param sysPay 物业费记录
     * @return 结果
     */
    @Override
    public int insertSysPay(SysPay sysPay)
    {
        sysPay.setUser(SecurityUtils.getUserId());
        return sysPayMapper.insertSysPay(sysPay);
    }

    /**
     * 修改物业费记录
     * 
     * @param sysPay 物业费记录
     * @return 结果
     */
    @Override
    public int updateSysPay(SysPay sysPay)
    {
        return sysPayMapper.updateSysPay(sysPay);
    }

    /**
     * 批量删除物业费记录
     * 
     * @param ids 需要删除的物业费记录主键
     * @return 结果
     */
    @Override
    public int deleteSysPayByIds(Long[] ids)
    {
        return sysPayMapper.deleteSysPayByIds(ids);
    }

    /**
     * 删除物业费记录信息
     * 
     * @param id 物业费记录主键
     * @return 结果
     */
    @Override
    public int deleteSysPayById(Long id)
    {
        return sysPayMapper.deleteSysPayById(id);
    }

    /**
     * 缴费
     * @param id
     * @return
     */
    @Override
    public int updateSysPay(Long id,String user) {
        SysUser sysUser = sysUserMapper.selectUserByUserName(user);
        Long userId = sysUser.getUserId();
        SysPay sysPay=new SysPay();
        sysPay.setId(id);
        sysPay.setUser(userId);

        //更新物品状态
        //hc_id
        SysPayVo sysPayVo = sysPayMapper.selectSysPayById(id);
        Long hcId = sysPayVo.getHcId();
        switch (sysPayVo.getType()){
            //物业费
            case "0":
                //更新房产状态
                sysHouseMapper.updateHouseNormal(hcId);
                break;
            //车位费
            case "1":
                //更新车辆状态
                sysCarMapper.updateSysCarNormal(hcId);
                break;
            //员工工资
            case "2":
                //更新员工工资状态
                sysStaffMapper.updateSysStaffNormal(hcId);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + sysPayVo.getType());
        }

        return sysPayMapper.updateSysPayStatus(sysPay);
    }


    /**
     * 每个月定时生成缴费记录
     *
     */
    @Scheduled(cron="30 10 4 1 * ?")   //每月1号4点10分30秒触发任务 "30 10 4 1 * ?"
                                        //每隔5秒执行一次："*/5 * * * * ?"
    public void addPayLog(){
        addHousePay();
        addCarPay();
        addStaffPay();
    }




    /**
     * 生成物业费待缴费记录
     */
    @Value("${wy.value}")
    private Long value;
    public void addHousePay(){
        //查询所有已经出售和出租的房子
        SysHouse sysHouse=new SysHouse();
        sysHouse.setStatus("1");
        List<SysHouseVo> sysHouseVos = sysHouseMapper.selectSysHouseList(sysHouse);
        sysHouse.setStatus("2");
        sysHouseVos.addAll(sysHouseMapper.selectSysHouseList(sysHouse));

        //为每一个使用中的房子生成物业费记录
        for (SysHouseVo sysHouseVo:sysHouseVos){
            Long size = sysHouseVo.getSize();
            Long valueAll=size*value;

            SysPay sysPay=new SysPay();
            sysPay.setType("0");
            sysPay.setValue(valueAll);
            sysPay.setHcId(sysHouseVo.getId());
            Calendar calendar = Calendar.getInstance();
            int year=calendar.get(Calendar.YEAR);
            int month=calendar.get(Calendar.MONTH) +1;
            sysPay.setRemark( year+"年"+month+"月"+"物业费");
            sysPay.setUser(sysHouseVo.getUser());
            sysPayMapper.insertSysPay(sysPay);

            //修改房子为欠费状态
            sysHouseMapper.updateHouseArrearage(sysHouseVo.getId());
        }
    }



    @Value("${wy.carport.rent}")
    //出租费
    private Long rent;

    @Value("${wy.carport.manage}")
    //管理费
    private Long manage;
    /**
     *生成车位费
     */
    @Autowired
    CarportViewMapper carportViewMapper;
    private void addCarPay() {
        //查询列表
        List<SysCarVo> sysCarVos = sysCarMapper.selectSysCarList(null);

        //生成文本信息
        Calendar calendar = Calendar.getInstance();
        int year=calendar.get(Calendar.YEAR);
        int month=calendar.get(Calendar.MONTH) +1;
        String carRemark = year+"年"+month+"月"+"车位费/管理费";

        //遍历列表计算车辆费用
        for (SysCarVo syscarVo:sysCarVos){
            SysPay sysPay=new SysPay();
            sysPay.setType("0");
            sysPay.setHcId(syscarVo.getId());
            sysPay.setRemark(carRemark);
            sysPay.setUser(syscarVo.getUser());

            //通过车辆id查询车位信息cid
            CarportView carportView=new CarportView();
            carportView.setcId( syscarVo.getId());
            List<CarportView> carportViews = carportViewMapper.selectCarportViewList(carportView);
            //按车位类型收费
            if(!carportViews.isEmpty()){
                if (Objects.equals(carportViews.get(0).getType(), "1")){
                    //出租型收出租费
                    sysPay.setValue(rent);
                }else if (Objects.equals(carportViews.get(0).getType(), "0")){
                    //购买型收管理费
                    sysPay.setValue(manage);
                }
            }
            sysPayMapper.insertSysPay(sysPay);
            //修改车辆为欠费状态
            sysHouseMapper.updateHouseArrearage(syscarVo.getId());
        }
    }

    /**
     * 生成员工工资记录
     *
     */
    public void addStaffPay(){

        //查询在职员工列表Staffs
        SysStaff sysStaff=new SysStaff();
        sysStaff.setStatus("0");
        List<SysStaffVo> Staffs = sysStaffMapper.selectSysStaffList(sysStaff);

        //生成员工工资记录
        for (SysStaffVo staff:Staffs){
            /* 计算工资 */
            Long payAll = staff.getPayAll();
            Long payAdd = staff.getPayAdd();
            Long payLose = staff.getPayLose();
            Long total=payAll+payAdd +payLose;
            Long user = staff.getUser();

            SysPay sysPay=new SysPay();
            Long leader=sysStaffMapper.selectLeaderByid(staff.getId());
            sysPay.setUser(leader);
            sysPay.setType("2");
            sysPay.setPayDate(new Date());
            sysPay.setValue(total);
            sysPay.setHcId(user);
            sysPay.setRemark("为"+user+"支付员工工资"+",奖励"+payAdd+"元,惩罚"+payLose+"元");
            sysPayMapper.insertSysPay(sysPay);
        }
    }

}
